/**
 * zmn产品模块-产品列表
 * @author tanghongjun
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [{
            title: '说明\n产品组展示：待生效产品组/已生效产品组',
            icon: 'layui-icon-help',
        }, 'filter', 'exports'],
        page: true,
        limit: json.pageSize,
        height: 'full-149'
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));

    zmnPage.laySelectLinkage(form, "/common/categ/listChilds.action", "categOneId", "categId", null);
    zmnPage.laySelectLinkage(form, "/product/productgroup/listVtByBizType.action", "bizType", "productGroupId", null);

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.groupId);
    });

    // 导出或下载
    table.on("toolbar(zmn-table)", function (obj) {
        var ids = new Array();

        //导出excel
        if (obj.event == 'CUSTOM_EXPORT') {
            zmnExportUtil.doExportExcel('zmn-table', '后台产品-分类关联列表');
            return;
        }

        let items = obj.config.data;
        for (var k = 0; k < items.length; k++) {
            var o = items[k];
            var isChecked = o["LAY_CHECKED"];
            if (isChecked) {
                ids.push(o['servCategId'] + '-' + o['categOneId'] + '-' + o['categId']);
            }
        }

        if (obj.event === "delete") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一列！");
            }
            layer.confirm('您确定要删除关联吗？', {
                btn: ['确定', '取消']
            }, function () {
                zmnPage.ajaxSimple("/serv/categ/ass/delete.action", {ids: ids.join(",")}, 1000);
            });
        }
    });


    $(".bth-operation").click(function () {
        let _this = $(this)
        let id = _this.attr("dataId");
        let item_a = "<a lay-href='" + "{url}" + id + "'" +
            " class='layui-btn layui-btn-sm layui-btn-normal'" +
            " lay-text='{text}' style='width:75px'>{text}" +
            "</a>"
        let item_span = "<span  class='layui-btn layui-btn-disabled layui-btn-sm' style='width:75px'>{text}</span>"
        let html = "<div class='layui-btn-container'>";
        let button_list = json.actionButtons
        for (let index in button_list) {
            let item = button_list[index]
            html += item.permiss ? item_a.replace("{url}", item.url).replaceAll("{text}", item.text)
                : item_span.replaceAll("{text}", item.text)
        }
        html += "</div>";
        indexTips = layer.tips(html, this, {
            shade: [0],
            shadeClose: true,
            area: ['370px', '90px'],
            time: 0,
            tips: [3, '#FFFFFF'],
        });

    });

    //对外暴露的接口
    exports('art_serv_ass_category_list', {});
});

function reload(msg) {
    parent.layui.layer.msg(msg);
    window.location.reload();
}

function toSetProductGroup(groupId) {
    layer.open({
        type: 2,
        title: '选择产品组',
        area: ['600px', '600px'],
        content: '/serv/categ/ass/addEditView.action?groupId=' + groupId
    });

}

function toEditDisclaimer(groupId) {
    layer.open({
        type: 2,
        title: '免责条款',
        area: ['600px', '390px'],
        content: '/serv/categ/ass/disclaimerView.action?groupId=' + groupId
    });
}